package com.lgx.mall.view.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.view.dao.IWebPageDetailDao;
import com.lgx.mall.view.entity.WebPageDetail;

@Component
public class WebPageDetailDao extends GeneralDao implements IWebPageDetailDao{

	protected Logger log = Logger.getLogger(this.getClass());
	

	public WebPageDetail createWebPageDetail(WebPageDetail gate)
			throws DataException {
		this.getHibernateTemplate().save(gate);
		log.info("[创建前台菜单:]"+gate.getId());
		return gate;
	}

	
	public WebPageDetail updateWebPageDetail(WebPageDetail gate)
			throws DataException {
		this.getHibernateTemplate().update(gate);
		log.info("[更新前台菜单:]"+gate.getId());
		return gate;
	}

	
	public void deleteWebPageDetail(WebPageDetail gate)
			throws DataException {
		log.info("[删除前台菜单:]"+gate.getId());
		this.getHibernateTemplate().delete(gate);
	}

	
	public void deleteWebPageDetail(long id) throws DataException {
		this.deleteWebPageDetail(this.findWebPageDetailById(id));
		log.info("[删除前台菜单:]"+id);
	}

	
	public WebPageDetail findWebPageDetailById(long id) {
		return this.getHibernateTemplate().get(WebPageDetail.class, id);
	}

	
	public Pagination findWebPageDetailPagination(String sql,
			Object[] params, int pageSize, int pageNum) {
		return this.getListPagination(sql, params, pageSize, pageNum);
	}

	
	public Pagination findWebPageDetailPagination(Object[] params,
			int pageSize, int pageNum) {
		String sql = "from WebPageDetail g where g.isDelete='0'";
		
		if(params[0] != null) {
			sql += " and g.categoryId ="+params[0]+"";
		}
		
		sql += " order by g.id asc";
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

	
	
	public void deleteWebPageDetails(String[] ids) throws DataException {
		for(String id : ids) {
			this.deleteWebPageDetail(Long.parseLong(id));
		}
	}


	@SuppressWarnings("unchecked")
	public List<WebPageDetail> findWebPageDetailByAreaAndCid(
			String areaNode, long categoryId) {
		
		String sql = "from WebPageDetail g where g.isDelete='0'";
		if(areaNode!=null && !(("").equals(areaNode))&&!("null").equals(areaNode)) {
			sql += " and g.areaNode="+areaNode+"";
		}
		if(categoryId!=0) {
			sql += " and g.categoryId="+categoryId;
		}
		sql += " order by g.id asc";
		return this.getHibernateTemplate().find(sql);
	}


	public Pagination findWebPageDetailPaginationByAreaAndCid(
			String areaNode, long categoryId, int pageSize, int pageNum) {

		Object[] params = new Object[10];
		String sql = "from WebPageDetail g where g.isDelete='0'";
		
		if(areaNode!=null && !(("").equals(areaNode))&&!("null").equals(areaNode)) {
			params[0] = areaNode;
			sql += " and g.areaNode="+areaNode+"";
		}
		if(categoryId!=0) {
			params[1] = categoryId;
			sql += " and g.categoryId="+categoryId;
		}
		sql += " order by g.id asc";
		
		return this.getListPagination(sql, new Object[]{}, pageSize, pageNum);
	}

}
